<template>
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="审核结果" prop="check_status">
          <el-radio-group v-model="form.check_status">
            <el-radio
              v-for="dict in dict.type.ext_check_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="原因" prop="check_reason" v-if="form.check_status == '2'">
          <el-input v-model="form.check_reason" type="textarea" rows="3" placeholder="请输入审核不通过原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
</template>

<script>
import { restGet, apiPut, apiPatch, apiDs } from "@/api/openapi";

export default {
  name: "CheckDialog",
  dicts: ['ext_check_status'],
  props:{
    //数据模型
      applyData: Object,
  },
  data() {
    return {
      open: false,
      title: '审批',
      form:{},
      // 表单校验
      rules: {
        check_status: [
          { required: true, message: "审批结果不能为空", trigger: "blur" }
        ],
        check_reason: [
          { required: true, message: "审批不通过时说明不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
  },
  watch: {
    applyData: function(newVal, oldVal){
      if(newVal == null){
        this.open = false;
      }else{
        this.reset();
        this.open = true;
        this.title = this.applyData.title;
      }
    }
  },
  methods: {
    reset() {
      this.form = {
        id:this.applyData.id,
        check_user_id: "@userId()",
        check_time: "@now()",
        check_status: '1',
        check_reason: null
      }
    },
    /** 取消按钮操作 */
    cancel() {
      this.$emit('cancel');
    },
    // 提交上传文件
    submit() { 
      this.$refs["form"].validate(valid => {
        if (valid) {          
          let data = { asset_flow: [], "@tags": [this.applyData["model"]] };
          data[this.applyData["model"]] = [{...this.form}];
          apiPut(data).then((response) => {
              this.$modal.msgSuccess("审批成功");
              this.open = false;
              this.$emit('success');
          });
        }
      });
    }
  }
};
</script>
